<template>
	<div ref="chartRef" :style="{ width, height }"></div>
</template>
<script lang="ts" setup>
import { Ref, ref } from 'vue';
import { useECharts } from '/@/hooks/web/useECharts';
import type { PropType } from 'vue';
import { deviceInstallCount } from '/@/api/analysis/index';
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
defineProps({
	width: {
		type: String as PropType<string>,
		default: '100%',
	},
	height: {
		type: String as PropType<string>,
		default: '100%',
	},
});

const chartRef = ref<HTMLDivElement | null>(null);
const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
const getDeviceInstallCount = () => {
	deviceInstallCount().then((res) => {
		// let total = 0,
		// 	data = [];
		// for (const k in res.data) {
		// 	data.push({
		// 		name: k,
		// 		value: res.data[k],
		// 	});
		// 	total += parseInt(res.data[k]);
		// }
		setOptions({
			tooltip: {
				trigger: 'item',
				position: 'top',
				formatter: '{b}: {d}%<br /> {c}',
			},
			legend: {
				// type: 'scroll',
				orient: 'vertical',
				itemWidth: 10,
				itemHeight: 5,
				textStyle: {
					fontSize: 12,
				},
				show: true,
				top: 0,
				left: 0,
			},
			grid: { left: 0, top: 0, bottom: 0, right: 0, containLabel: true },
			graphic: [
				{
					type: 'text',
					top: 'center',
					left: 'center',
					style: {
						text: `${res.data.deviceTotal}`,
						fontSize: 18,
						fontWeight: 700,
					},
					tooltip: {
						show: false,
					},
				},
			],
			series: [
				{
					type: 'pie',
					z: 999,
					radius: ['50%', '70%'],
					center: ['50%', '50%'],
					color: ['#00c997', '#4c84ff'],
					minAngle: 2,
					// startAngle: 120,
					itemStyle: {
						borderColor: '#fff',
						borderWidth: 1,
					},
					label: {
						show: true,
						formatter: '{b}:{c}\n{d}%',
					},
					data: [
						{
							name: t('dashboard.installed'),
							value: res.data.cutboundCount,
						},
						{
							name: t('dashboard.uninstalled'),
							value: res.data.unCutboundCount,
						},
					],
					labelLine: {
						show: true,
					},
					animationType: 'scale',
					animationEasing: 'exponentialInOut',
					animationDelay: function () {
						return Math.random() * 400;
					},
				},
			],
		});
	});
};
onMounted(() => {
	getDeviceInstallCount();
});
</script>
